/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.presentationorganization;

import org.openaecfm.model.presentationappearance.NullStyle;
import org.openaecfm.model.presentationappearance.PresentationStyle;
import org.openaecfm.model.presentationorganization.PresentationLayerAssignment;
import org.openaecfm.model.presentationorganization.PresentationLayerWithStyle;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.TypedParameter;

public final class PresentationLayerWithStyleIO extends
		AbstractEntityInstanceInputImpl<PresentationLayerWithStyle> implements
		IEntityInstanceOutput<PresentationLayerWithStyle> {
	private IEntityInstanceUtilInput<PresentationLayerAssignment> utilInput;
	private IEntityInstanceUtilOutput<PresentationLayerAssignment> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<PresentationLayerAssignment> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(PresentationLayerWithStyle pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCPRESENTATIONLAYERWITHSTYLE", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set LayerOn
		ei.addParameter(new EnumerationParameter(pojo.getLayerOn()));

		// Set LayerFrozen
		ei.addParameter(new EnumerationParameter(pojo.getLayerFrozen()));

		// Set LayerBlocked
		ei.addParameter(new EnumerationParameter(pojo.getLayerBlocked()));

		// Set LayerStyles
		ListParameter layers = new ListParameter();
		for (Object e : pojo.getLayerStyles()) {
			if (e instanceof NullStyle) {
				TypedParameter t = new TypedParameter("IFCNULLSTYLE",
						new EnumerationParameter("."
								+ ((NullStyle) e).toString() + "."));
				layers.add(t);
			} else {
				layers.add(new EntityInstanceNameParameter(
						appDataManager.getName(e)));
			}
		}
		ei.addParameter(layers);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected PresentationLayerWithStyle doCreateObject() {
		PresentationLayerWithStyle pojo = new PresentationLayerWithStyle();

		return pojo;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			PresentationLayerWithStyle pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set LayerStyles
		ListParameter styles = (ListParameter) sei.getParameter(7);
		for (Parameter p : styles) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
			pojo.getLayerStyles().add(
					(PresentationStyle) appDataManager.getObject(n
							.toInstanceName()));
		}
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<PresentationLayerAssignment> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			PresentationLayerWithStyle pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set LayerOn
		EnumerationParameter on = (EnumerationParameter) sei.getParameter(4);
		pojo.setLayerOn(org.openaecfm.model.measure.Logical.valueOf(on
				.toEnumString()));

		// Set LayerFrozen
		EnumerationParameter frozen = (EnumerationParameter) sei
				.getParameter(5);
		pojo.setLayerFrozen(org.openaecfm.model.measure.Logical.valueOf(frozen
				.toEnumString()));

		// Set LayerBlocked
		EnumerationParameter blocked = (EnumerationParameter) sei
				.getParameter(6);
		pojo.setLayerBlocked(org.openaecfm.model.measure.Logical
				.valueOf(blocked.toEnumString()));
	}
}
